package uk.ac.manchester.tewpl.iceiface.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import uk.ac.manchester.tewpl.iceiface.client.curriculummap.Map;
import uk.ac.manchester.tewpl.iceiface.client.explorer.Explorer;
import uk.ac.manchester.tewpl.iceiface.client.lists.Lists;
import uk.ac.manchester.tewpl.iceiface.client.model.KBCommunityModel;
import uk.ac.manchester.tewpl.iceiface.client.model.KBWorkspaceModel;
import uk.ac.manchester.tewpl.iceiface.client.service.CommunityService;
import uk.ac.manchester.tewpl.iceiface.client.service.CommunityServiceAsync;
import uk.ac.manchester.tewpl.iceiface.client.service.KBService;
import uk.ac.manchester.tewpl.iceiface.client.service.KBServiceAsync;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.desktop.client.StartMenu;
import com.extjs.gxt.desktop.client.TaskBar;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.SplitButton;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.custom.ThemeSelector;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.menu.CheckMenuItem;
import com.extjs.gxt.ui.client.widget.menu.DateMenu;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.widgetideas.graphics.client.Color;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Application implements EntryPoint {
	
	/*
	 * Instanciate the Services
	 */
	KBServiceAsync kbservice = GWT.create(KBService.class);
	CommunityServiceAsync cservice = GWT.create(CommunityService.class);
	
	/*
	 * Set up environment
	 */
	public void onModuleLoad() {
		// Add initial viewport
		final Viewport vp = new Viewport();
		RootPanel.get().add(vp);
		
		// Register services
		vp.mask("Loading...");
		Registry.register("kbservice", kbservice);
		Registry.register("cservice", cservice);

		// Get the community TODO hardcoded Community ID = 1
		vp.mask("Loading Community...");		
		cservice.findCommunityById(new Long(1), new AsyncCallback<KBCommunityModel>() {
			@Override
			public void onFailure(Throwable arg0) {
				vp.mask("Unable to access Community!");
			}
			@Override
			public void onSuccess(KBCommunityModel c) {
				//put the Community in the Registry
				Registry.register("community", c);
				
				//get the workspace
				vp.mask("Loading Workspace...");
				kbservice.findWorkspacesByCommunity(c, new AsyncCallback<Collection<KBWorkspaceModel>>() {
					@Override
					public void onSuccess(Collection<KBWorkspaceModel> wss) {
						for(KBWorkspaceModel ws : wss){
							//hardcoded Workspace URI:
							if (ws.getUri().equals("workspace:Test:CRAMPON")
									|| ws.getUri().equals("workspace:Manchester:CRAMPON")){
								//put the Workspace in the registry
								Registry.register("workspace", ws);
								
								RootPanel.get().remove(vp);
								
								// Load relevant UI
								populateDesktop();
								//viewportMode();
								
							}
							else {
								vp.mask("Workspace would not be found!");
							}
						}
					}
					@Override
					public void onFailure(Throwable arg0) {
						vp.mask("Unable to access Workspace!");
					}
				});
			}
		});

	}
	
	/*
	 * Desktop mode
	 */
	
	private CustomDesktop desktop;
	
	private void populateDesktop() {

		SelectionListener<MenuEvent> menuListener = new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent me) {
				itemSelected(me);
			}
		};

		SelectionListener<ComponentEvent> shortcutListener = new SelectionListener<ComponentEvent>() {
			@Override
			public void componentSelected(ComponentEvent ce) {
				itemSelected(ce);
			}
		};
		desktop = new CustomDesktop();
		desktop.getDesktop().setStyleAttribute("background-color", "#000033");
		//TaskBar taskBar = desktop.getTaskBar();
		
		// Add curriculum map desktop shortcut
		Shortcut curriculumMapShortcut = new Shortcut();
		curriculumMapShortcut.setText("Curriculum Explorer");
		curriculumMapShortcut.setId("cmap-shortcut");
		curriculumMapShortcut.setData("window", new ExplorerWindow());
		curriculumMapShortcut.addSelectionListener(shortcutListener);
		desktop.addShortcut(curriculumMapShortcut);
		
		
		// Add curriculum map desktop shortcut
		Shortcut listsMapShortcut = new Shortcut();
		listsMapShortcut.setText("Elastic Lists");
		listsMapShortcut.setId("lists-shortcut");
		listsMapShortcut.setData("window", new ListsWindow());
		listsMapShortcut.addSelectionListener(shortcutListener);
		desktop.addShortcut(listsMapShortcut);
		
	}
	
	private void itemSelected(ComponentEvent ce) {
		Window w;
		if (ce instanceof MenuEvent) {
			MenuEvent me = (MenuEvent) ce;
			w = me.getItem().getData("window");
		} else {
			w = ce.getComponent().getData("window");
		}
		if (!desktop.getWindows().contains(w)) {
			desktop.addWindow(w);
		}
		if (w != null && !w.isVisible()) {
			w.show();
		} else {
			w.toFront();
		}
	}
	
	/*
	 * Viewport Embed mode
	 */
	
	private void viewportMode() {
		Viewport vp = new Viewport();
		vp.setLayout(new FitLayout());
		Explorer explorer = new Explorer();
		//Lists explorer = new Lists();
		
		vp.add(explorer);
		
		RootPanel.get().add(vp);
		
		explorer.init();
	}
	
	
	/*
	 * Embed Mode
	 */

	private GWTCanvas canvas = new GWTCanvas(200,200);
	
	private static Application instance;
	
	public void onModuleLoad1() {	
		//ContentPanel cp = new ContentPanel();
		//cp.setHeaderVisible(true);
		//cp.setHeading("ICEiFace");
		//cp.setLayout(new FitLayout());
		//cp.setSize(400,400);
		

	    canvas.setBackgroundColor(Color.DARK_BLUE);
	    //viewport.add(canvas);
	    Viewport viewport = new Viewport();
		viewport.setLayout(new FitLayout());
	    Explorer explorer = new Explorer();
	    viewport.add(explorer);
	    explorer.init();
	    viewport.layout();
	    //viewport.add(new Button("hello"));
	    
	   // cp.add(canvas);

	    //RootPanel.get().add(cp);
	    
		
	    RootPanel.get().add(viewport);
	    
	    //instance = this;
	    //exportStaticMethod();
	}
	
	public void doit() {
	    canvas.setFillStyle(Color.RED);
	    
	    canvas.beginPath();
	    	canvas.arc(100, 100, 24, 0, Math.PI * 2, true);
	    canvas.fill();
	}
	public static native void exportStaticMethod() /*-{
		$wnd.doit = @uk.ac.manchester.tewpl.iceiface.client.Application::jsniDoit(Ljava/lang/String;);
	}-*/;
	
	public static void jsniDoit(String x) {
		instance.doit();
		MessageBox.info("", "You said: " + x, null);
	}
	
}
